<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <span>编辑标签</span>
        <el-button style="float: right;" @click="goToList">返回列表</el-button>
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model="form.tagName" placeholder="请输入标签名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="goToList">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getTag, updateTag } from "@/api/cms/tag";

export default {
  name: "TagEdit",
  data() {
    return {
      // 表单参数
      form: {
        tagId: null,
        tagName: null,
        remark: null
      },
      // 表单校验
      rules: {
        tagName: [
          { required: true, message: "标签名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const tagId = this.$route.params.tagId;
    this.getTagInfo(tagId);
  },
  methods: {
    /** 获取标签详情 */
    getTagInfo(tagId) {
      getTag(tagId).then(response => {
        this.form = response.data;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        tagId: null,
        tagName: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateTag(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.goToList();
          });
        }
      });
    },
    /** 返回列表 */
    goToList() {
      this.$router.push('/cms/tag');
    }
  }
};
</script>